bpo-44904: Fix classmethod property bug in doctest module#28838
bpo-44904: Fix classmethod property bug in doctest module#28838serhiy-storchaka merged 2 commits intopython:mainfrom
Conversation
The doctest module raised an error if a docstring contained an example that attempted to access a classmethod property. (Stacking '@classmethod' on top of `@property` has been supported since Python 3.9; see https://docs.python.org/3/howto/descriptor.html#class-methods.)
Lib/doctest.py
Outdated
| val = getattr(obj, valname) | ||
| if isinstance(val, classmethod): | ||
| val = getattr(obj, valname).__func__ | ||
| # Lookup via __dict__ instead of getattr |
There was a problem hiding this comment.
This PR should not go forward until issue 45356 is resolved. Wrapping classmethod around property seems to have some intrinsic flaws that shouldn't be papered over.
Also, when replacing getattr with a dict lookup, we need to consider whether the entire __mro__ should be searched.
Lastly, any "solution" to this or the help() bug should probably share a standardized solution, perhaps some variant of hasattr() that doesn't have the __getattr__ hook and that doesn't trigger descriptor behavior.
There was a problem hiding this comment.
This all makes sense. Thanks for taking a look, anyhow!
There was a problem hiding this comment.
__mro__ is not related here. We iterate the class' __dict__ and look at methods defined in the class.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
Lib/doctest.py
Outdated
| val = getattr(obj, valname) | ||
| if isinstance(val, classmethod): | ||
| val = getattr(obj, valname).__func__ | ||
| # Lookup via __dict__ instead of getattr |
There was a problem hiding this comment.
__mro__ is not related here. We iterate the class' __dict__ and look at methods defined in the class.
|
@rhettinger, I am going to merge this issue. Do you still have objections? |
|
Even if we will deprecate class properties in future, the changes of this PR make the code simpler and compatible with other decorators. |
|
Thanks @AlexWaygood for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9, 3.10. |
|
GH-29261 is a backport of this pull request to the 3.10 branch. |
|
GH-29262 is a backport of this pull request to the 3.9 branch. |
…8838) The doctest module raised an error if a docstring contained an example that attempted to access a classmethod property. (Stacking '@classmethod' on top of `@property` has been supported since Python 3.9; see https://docs.python.org/3/howto/descriptor.htmlGH-class-methods.) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> (cherry picked from commit b1302ab) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
…8838) The doctest module raised an error if a docstring contained an example that attempted to access a classmethod property. (Stacking '@classmethod' on top of `@property` has been supported since Python 3.9; see https://docs.python.org/3/howto/descriptor.htmlGH-class-methods.) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> (cherry picked from commit b1302ab) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
The doctest module raised an error if a docstring contained an example that attempted to access a classmethod property. (Stacking '@classmethod' on top of `@property` has been supported since Python 3.9; see https://docs.python.org/3/howto/descriptor.htmlGH-class-methods.) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> (cherry picked from commit b1302ab) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
The doctest module raised an error if a docstring contained an example that attempted to access a classmethod property. (Stacking '@classmethod' on top of `@property` has been supported since Python 3.9; see https://docs.python.org/3/howto/descriptor.htmlGH-class-methods.) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> (cherry picked from commit b1302ab) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
|
The doctest module raised an error if a docstring contained an example that
attempted to access a classmethod property. (Stacking
@classmethodon top of@propertyhas been supported since Python 3.9; seehttps://docs.python.org/3/howto/descriptor.html#class-methods.)
https://bugs.python.org/issue44904